home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n4.arc
/
TXT2WP51.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
10KB
|
266 lines
/*---- TXT2WP51.C ------------------------- Listing 2 -----
* Convert ASCII text files to WordPerfect 5.1
* by Andrew Binstock, v. 1.3
*
* Verified for Borland C/C++, Microsoft 6.00A
*
* Cpoyright 1991, Andrew Binstock. May be used freely if
* authorship and publication are acknowledged.
*
* Usage: TXT2WP51 infile outfile
*-------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include "wpprefix.h"
FILE *fin, *fout;
struct wp_prefix prefix; /* from the wpprefix.h header */
struct wp_addl_prefix addl_prefix;
struct wp_packet_index packet;
void main ( int argc, char *argv[] )
{
int last_char_out;
WORD data_array [4];
if ( argc != 3 )
{
fputs ( "Invalid file specification. Usage:\n", stderr );
fputs ( "\n\tTXT2WP51 infile outfile\n\n", stderr );
fputs ( "infile = textfile, outfile = WordPerfect file",
stderr );
exit ( 3 );
}
if (( fin = fopen ( argv[1], "rb" )) == NULL )
{
fprintf ( stderr, "Unable to open: %s\n", argv[1] );
exit ( 4 );
}
/*---------------------------------------------------------
* Before overwriting output file, let's make sure
*-------------------------------------------------------*/
if (( fout = fopen ( argv[2], "rb" )) != NULL )
{
int yes_no;
fclose ( fout );
fprintf ( stderr, "\n%s exists. Overwrite? (Y/N) .\b",
argv[2] );
yes_no = getch ();
if ( yes_no != 'Y' && yes_no != 'y' )
exit ( 5 );
}
fout = fopen ( argv[2], "wb" );
/*---------------------------------------------------------
* Is it already a WordPerfect file? If so, get out.
*-------------------------------------------------------*/
fread ( &prefix, sizeof ( struct wp_prefix ), 1, fin );
if ( prefix.id[0] == -1 && /* Test for WP signature */
prefix.id[1] == 'W' &&
prefix.id[2] == 'P' &&
prefix.id[3] == 'C' )
{
fprintf ( stderr,
"%s is already a WordPerfect document\n",
argv[1] );
unlink ( argv[2] ); /* delete the opened output file */
exit ( 6 );
}
else
rewind ( fin );
/*---------------------------------------------------------
* So far, so good. Now, set up and write the prefix.
*-------------------------------------------------------*/
prefix.id[0] = -1;
prefix.id[1] = 'W';
prefix.id[2] = 'P';
prefix.id[3] = 'C';
prefix.doc_start = 76L; /* length of prefix plus the
packets = 76 bytes. */
prefix.prod_type = 1;
prefix.file_type = 0x0A;
prefix.maj_version = 0;
prefix.min_version = 1; /* for WP5.0, set to 0 */
prefix.encrypted = 0; /* no encryption here */
prefix.filler = 0;
if (( fwrite ( &prefix, sizeof ( prefix ), 1, fout )) != 1 )
{
fprintf ( stderr, "Error writing to %s\n", argv[2] );
exit ( 7 );
}
/*---------------------------------------------------------
* Now write out the additional prefix.
*-------------------------------------------------------*/
addl_prefix.packet_type = 0xFFFB;
addl_prefix.indices = 5;
addl_prefix.index_size = 50;
addl_prefix.next_block = 0L;
fwrite ( &addl_prefix, sizeof ( addl_prefix ), 1, fout );
/*---------------------------------------------------------
* Now write out the packet indices.
*-------------------------------------------------------*/
/* == Index to Packet 1 == */
packet.type = 6; /* 6 = document-specific data */
packet.length = 8L; /* points to 8 bytes of data */
packet.data_offset = 66L; /* data begins at byte 66 */
fwrite ( &packet, sizeof ( packet ), 1, fout );
/* == Index to Packet 2 == */
packet.type = 8; /* 8 = graphics data */
packet.length = 2L; /* points to 2 bytes of data */
packet.data_offset = 74L; /* data begins at byte 74, */
/* i.e., right after data of */
/* previous packet. */
fwrite ( &packet, sizeof ( packet ), 1, fout );
/* == Indices to Packets 3 & 4 == */
packet.type = 0; /* end of packets */
packet.length = 0L;
packet.data_offset = 0L;
fwrite ( &packet, sizeof ( packet ), 1, fout ); /* #3 */
fwrite ( &packet, sizeof ( packet ), 1, fout ); /* #4 */
/*---------------------------------------------------------
* At this point we have written out:
* prefix = 16 bytes
* additional prefix = 10 bytes
* 4 packets @ 10 bytes = 40 bytes
* --
* Total 66 bytes.
*
* Hence, byte 66 (couting from zero) is the beginning
* of the data area. Note that this is the byte pointed
* to by packet #1's data_offset.
*-------------------------------------------------------*/
/* the 8 bytes of data pointed to by packet 1
consist of four words:
word 0: a series of bit-field flags.
when bit 4 (from 0) is set to 1,
this tells WP to regenerate the document;
all other bits shoud be set to 0.
word 1: redline character width
use WP default of 124;
word 2: width of screen character
use WP default of 120;
word 3: unused, set to 0. */
data_array[0] = 0x08;
data_array[1] = 124;
data_array[2] = 120;
data_array[3] = 0;
fwrite ( data_array, sizeof ( WORD ) * 4, 1, fout );
/* the two bytes of data pointed to by packet 2
consist of one word:
if = 0, no graphics in this document;
if = 2, points to graphics data; */
data_array[0] = 0;
fwrite ( data_array, sizeof ( WORD ), 1, fout );
/*---------------------------------------------------------
* Now write out the document.
*-------------------------------------------------------*/
last_char_out = '\0'; /* make sure it's not a space
to start with */
for (;;)
{
int c;
if (( c = fgetc ( fin )) == EOF )
break;
/* Disable the following translation if you do NOT want
to have WordPerfect do the word-wrapping for the file.
Should be left alone for documents, but disabled
for columnar data, source code files, poetry, etc.
Here's how it works:
0x0A (LF) is the hard return for WP. A line break
here is mandatory.
0x0D (CR) is a soft return for WP. That is, one that
can be moved around to accommodate word
wrapping functions. */
#define CR 0x0D
#define LF 0x0A
if ( c == CR ) /* the start of a text CR/LF combination*/
{
c = fgetc ( fin ); /* get the next character */
if ( c != LF ) /* is it the LF we expect? */
{ /* No!, so pass it all through */
fputc ( CR, fout );
last_char_out = fputc ( c, fout );
}
else /* It was a CR/LF. It will be soft */
{ /* if text follows. */
c = fgetc ( fin );
if ( isspace ( c )) /* followed by whitespace? */
{
fputc ( LF, fout ); /* yes, so write a hard*/
last_char_out = fputc ( c, fout ); /*return*/
}
else
{
if ( last_char_out != ' ' ) /* No, so write*/
fputc ( CR, fout ); /* a soft return.*/
ungetc ( c, fin ); /* Put back that */
} /* last character*/
}
}
else
{
/* Any translation of formatting data should go here */
/* As an example, here is how ASCII characters with the
8th bit set are translated. First, find out which WP
character is the equivalent of the character being
translated. Then specify it, by embedding the number
of the character and the character set into a 4-byte
escape sequence:
C0h char char-set C0h
In this example, we translate the character for 1/2
(ABh): which is character 17 in WP character set 4.
Most books on WP gives the full list of translations.
So... */
if ( c == 0xAB )
{
fprintf ( fout, "%c%c%c%c",
0xC0, 0x11, 0x04, 0xC0 );
last_char_out = c;
}
else
last_char_out = fputc ( c, fout );
}
}
fclose ( fin );
fclose ( fout );
exit (EXIT_SUCCESS); /* defined in stdlib.h--all went well */
}